import {getJobList} from '../../api/jobs';
Page({

  /**
   * 页面的初始数据
   */
  data: {

      jobTypeIcon: "cuIcon-unfold",
      jobLocationIcon: "cuIcon-unfold",
      educationIcon: "cuIcon-unfold",
      genderIcon: "cuIcon-unfold",
      workTypeIcon: "cuIcon-unfold",
      isModalVisible : false,
      loading : true,
      // Picker 数据
      pickerValue: 0,
      pickerRange: [],
      pickerType: "",

      // 底部弹出层数据
      isBottomSheetVisible: false,
      sheetType: "",
      job_type_list : ['全部岗位','普通岗位','灵活就业'],
      job_type      : '全部岗位',
      area_type_list : ["全部岗位", "本地岗位", "外地岗位"],
      area_type      : '全部岗位',
      educationList: ["不限", "中专", "大专", "本科", "硕士", "博士"],
      genderList: ["男", "女", "男女不限"],
      selectedEducation: "不限",
      selectedGender: "男女不限",
      work_type_list : [
        '物流配送','营销宣传','行政','服务业','市场营销','餐饮行业','影视传媒'
      ],
      work_list : [
        '分拣员','快递员','跑腿代办','送货司机','安保','建筑/房地产','驾驶员','装卸/搬运','驾驶员','操作工','物流','司机','交通/物流/仓储'
      ],
      isBottomSheetVisible:false,//工种弹窗
      selectedWorkType:"",
      selectedWork:"",
      jobList: [
       
      ],
      pageNo : 1,
      pageSize : 10,
      loading: false,
		  noMoreData: false,
  },
   contact(){
    this.setData({
      isModalVisible : true
    })
   },
   hideModal(){
    this.setData({
        isModalVisible : false
      })
   },
   nav_to(e){
    const url = e.currentTarget.dataset.url
    wx.reLaunch({
      url: url,
    })
  },
  //关闭弹窗
    closePopup() {
      this.setData({
        isBottomSheetVisible: false,
      });
    },

    //跳转岗位
    view_job(event){
        var id = event.currentTarget.dataset.id
            
        wx.navigateTo({
            url: '/pages/companies/job_detail?id='+id,
        })
    },
    
    //区域选择
    toggleAreaPicker(e){
        
      const index = e.detail.value
    
      this.setData({
        area_type : this.data.area_type_list[index]
      })
  },
  //切换岗位
    toggleJobPicker(e){
       
        const index = e.detail.value
      
        this.setData({
          job_type : this.data.job_type_list[index]
        })
    },
 

        // Picker 改变
    onPickerChange(e) {
      const selectedValue = this.data.pickerRange[e.detail.value];
      console.log(`选中的值为：${selectedValue}`);
    },


  // 显示底部弹出层
  showBottomSheet(e) {
    const type = e.currentTarget.dataset.type;
    const iconKey = `${type}Icon`;
    this.setData({
      sheetType: type,
      isBottomSheetVisible: true,
      [iconKey]: "cuIcon-fold",
    });
  },

    // 选择弹出层选项
    selectSheetOption(e) {
      const value = e.currentTarget.dataset.value;
      if (this.data.sheetType === "education") {
        this.setData({ selectedEducation: value });
      } else if (this.data.sheetType === "gender") {
        this.setData({ selectedGender: value });
      }
    },

    //选择岗位类型
    select_work_type(e){
      const value = e.currentTarget.dataset.value;
      this.setData({ selectedWorkType: value });
    },

    //选择职位
    select_work(e){
      const value = e.currentTarget.dataset.value;
      this.setData({ selectedWork: value });
    },

      // 确认弹出层
  confirmBottomSheet() {
    console.log(`确认选择：${this.data.selectedEducation || this.data.selectedGender}`);
    this.closeBottomSheet();
  },

    // 关闭弹出层
    closeBottomSheet() {
      const iconKey = `${this.data.sheetType}Icon`;
      this.setData({
        isBottomSheetVisible: false,
        [iconKey]: "cuIcon-unfold",
      });
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadMoreData()
  },

  onReachBottom() {
    this.loadMoreData()
  },

  // 异步加载数据
  async loadMoreData() {
    // 防止重复加载和无更多数据时继续加载
    if (this.data.loading || this.data.noMoreData) return;

    // 设置加载状态
    this.setData({ loading: true });

    try {
      // 获取新数据
      const newItems = await this.getListData();
      if (newItems.length === 0) {
        // 如果没有数据，标记无更多数据
        this.setData({ noMoreData: true });
      } else {
        // 将新数据追加到现有列表中
        this.setData({
          jobList: [...this.data.jobList, ...newItems],
          pageNo: this.data.pageNo + 1, // 页码递增
        });
      }
    } catch (error) {
      console.error('加载更多数据失败:', error);
    } finally {
      // 关闭加载状态
      this.setData({ loading: false });
    }
  },

  //获取数据
  async getListData() {
    try {
      // 调用接口获取数据
      const response = await getJobList({
        pageSize: this.data.pageSize,   
        pageNo: this.data.pageNo, 
      });
      if (response && response.data && response.data.list) {
        return response.data.list; // 返回数据列表
      } else {
        throw new Error('数据格式错误');
      }
    } catch (error) {
      return []; // 返回空数组，避免后续逻辑错误
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})